//
//  DLBaseNavigationController.m
//  DGGUIKit
//
//  Created by dgg_hzk on 2019/11/21.
//

#import "DLBaseNavigationController.h"

@interface DLBaseNavigationController ()<UIGestureRecognizerDelegate>

@end

@implementation DLBaseNavigationController

+ (instancetype)allocWithZone:(struct _NSZone *)zone {
    DLBaseNavigationController *viewController = [super allocWithZone:zone];
    @weakify(viewController)
    [[viewController rac_signalForSelector:@selector(viewWillAppear:)] subscribeNext:^(id x) {
        @strongify(viewController)
        [viewController dgg_layoutNavigation];
    }];
    return viewController;
}
    
- (void)dealloc {
    NSLog(@"♦️释放♦️ %@ '%@'", NSStringFromSelector(_cmd), self.class);
}
    
/**
 设置导航
 */
- (void)dgg_layoutNavigation{}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:18]}];
    self.interactivePopGestureRecognizer.enabled = YES;
    self.interactivePopGestureRecognizer.delegate = self;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
    
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated{
    if (self.childViewControllers.count >0) {
        if (!viewController.navigationItem.leftBarButtonItem) {
            viewController.navigationItem.leftBarButtonItem =  [self addBackItem];
        }
        viewController.hidesBottomBarWhenPushed = YES;
    }
    [super pushViewController:viewController animated:animated];
}
    
- (UIBarButtonItem*)addBackItem{
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [backButton setImage:[UIImage imageNamed:kDLUIKitImageName(@"DLUIKit_Nav_Return")] forState:(UIControlStateNormal)];
    [backButton setImage:[UIImage imageNamed:kDLUIKitImageName(@"DLUIKit_Nav_Return")] forState:(UIControlStateHighlighted)];
    [backButton setTitle:@"" forState:(UIControlStateNormal)];
    [backButton setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
    [backButton setTitleColor:[UIColor whiteColor] forState:(UIControlStateHighlighted)];
    backButton.contentEdgeInsets = UIEdgeInsetsMake(0, -6, 0, 0);
    [backButton sizeToFit];
    [backButton addTarget:self action:@selector(back) forControlEvents:(UIControlEventTouchUpInside)];
    backButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    backButton.bounds = CGRectMake(0, 0, kBarHeight, kBarHeight);
    return  [[UIBarButtonItem alloc] initWithCustomView:backButton];
}
    
- (void)back{
    //取消注册所有路由
//    [[JLRoutes globalRoutes] removeAllRoutes];
    SEL sel = NSSelectorFromString(@"dgg_backBtnClick");
    UIViewController *vc = self.childViewControllers.lastObject;
    if ([vc respondsToSelector:sel]) {
        ((void (*)(id, SEL))[vc methodForSelector:sel])(vc, sel);
        return;
    }
    [self popViewControllerAnimated:YES];
}
    
#pragma mark - 设置滑动返回-
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
    if (self.viewControllers.count == 1)//关闭主界面的右滑返回
    {
        return NO;
    }else{
        return YES;
    }
}

#pragma mark - 屏幕方向切换
//是否可以旋转
- (BOOL)shouldAutorotate{
    return self.topViewController.shouldAutorotate;
}

//支持的方向
- (UIInterfaceOrientationMask)supportedInterfaceOrientations{
    return self.topViewController.supportedInterfaceOrientations;
}

@end
